import Mock from 'mockjs'

  // 热门手机网络质量排名数据
  export const mockPhoneNetworkQualityRank = () => {
    Mock.mock(/\/api\/phone\/Rank/, 'get', (options) => {
      const url = new URL(options.url, window.location.origin)
      const params = Object.fromEntries(url.searchParams)

      // 基础数据
      const baseData = [25000, 14000, 5000, 5000]

      // 根据网络制式返回不同的数据
      let seriesData
      switch(params.networkType) {
        case '4G':
          seriesData = baseData.map(val => val * 0.6)
          break
        case '3G':
          seriesData = baseData.map(val => val * 0.3)
          break
        case '2G':
          seriesData = baseData.map(val => val * 0.1)
          break
        default: // ALL
          seriesData = baseData
      }

      return {
        code: 200,
        message: 'success',
        data: {
          legends: ['ALL', '4G', '3G', '2G'],
          xAxisData: ['iPhone 14', 'Huawei P60', 'OPPO Find X6', 'vivo X90'],
          seriesData: [seriesData]
        }
      }
    })
  }

// OS分布数据
export const mockOSDistribution = () => {
  Mock.mock(/\/api\/phone\/os-distribution/, 'get', (options) => {
    return {
      code: 200,
      message: 'success',
      data: {
        cities: [
          { name: 'Android 14', longitude: 116.46, latitude: 39.92, value: '14.0.0' },
          { name: 'Android 13', longitude: 121.48, latitude: 31.22, value: '13.1.2' },
          { name: 'Android 12', longitude: 113.23, latitude: 23.16, value: '12.0.1' },
          { name: 'Android 11', longitude: 120.13, latitude: 30.23, value: '11.2.0' },
          { name: 'Android 10', longitude: 108.95, latitude: 34.27, value: '10.0.0' },
          { name: 'Android 9', longitude: 104.06, latitude: 30.67, value: '9.0.1' }
        ]
      }
    }
  })
}

// 手机分布数据
export const mockPhoneDistribution = () => {
  Mock.mock(/\/api\/phone\/phone-distribution/, 'get', (options) => {
    return {
      code: 200,
      message: 'success',
      data: {
        cities: [
          { name: 'HUAWEI Mate60', longitude: 116.46, latitude: 39.92},
          { name: 'OPPO Find X7', longitude: 121.48, latitude: 31.22},
          { name: 'VIVO X100', longitude: 113.23, latitude: 23.16},
          { name: 'Xiaomi 14', longitude: 120.13, latitude: 30.23},
          { name: 'HONOR 90', longitude: 108.95, latitude: 34.27},
          { name: 'iQOO 12', longitude: 104.06, latitude: 30.67}
        ]
      }
    }
  })
}

// 修改分布数据API
export const mockPhoneTrafficDistribution = () => {
  Mock.mock(/\/api\/phone\/distribution/, 'get', (options) => {
    const data = {
      '小米': [
        { coord: [120.123, 30.240,], value: 53 },
        { coord: [120.124, 30.241], value: 64 },
        { coord: [120.125, 30.242], value: 41 },
        { coord: [120.126, 30.243], value: 82 },
        { coord: [120.127, 30.244], value: 45 },
        { coord: [120.128, 30.245], value: 68 },
        { coord: [120.129, 30.246], value: 39 },
        { coord: [120.130, 30.247], value: 75 },
        { coord: [120.131, 30.248], value: 50 },
        { coord: [120.132, 30.249], value: 39 },
        { coord: [120.133, 30.250], value: 65 },
        { coord: [120.134, 30.251], value: 60 },
        { coord: [120.135, 30.252], value: 54 },
        { coord: [120.136, 30.253], value: 41 },
        { coord: [120.137, 30.254], value: 83 },
        { coord: [120.138, 30.255], value: 91 },
        { coord: [120.139, 30.256], value: 72 },
        { coord: [120.140, 30.257], value: 44 },
        { coord: [120.141, 30.258], value: 66 },
        { coord: [120.142, 30.259], value: 51 },
        { coord: [120.143, 30.260], value: 38 },
        { coord: [120.144, 30.261], value: 61 },
        { coord: [120.145, 30.262], value: 73 },
        { coord: [120.146, 30.263], value: 55 },
        { coord: [120.147, 30.264], value: 49 },
        { coord: [120.148, 30.265], value: 62 },
        { coord: [120.149, 30.266], value: 78 },
        { coord: [120.150, 30.267], value: 54 },
        { coord: [120.151, 30.268], value: 69 },
        { coord: [120.152, 30.269], value: 81 },
        { coord: [120.153, 30.270], value: 56 },
        { coord: [120.154, 30.271], value: 47 },
        { coord: [120.155, 30.272], value: 72 },
        { coord: [120.156, 30.273], value: 65 },
        { coord: [120.157, 30.274], value: 54 },
        { coord: [120.158, 30.275], value: 49 },
        { coord: [120.159, 30.276], value: 39 },
        { coord: [120.160, 30.277], value: 58 },
        { coord: [120.161, 30.278], value: 62 },
        { coord: [120.162, 30.279], value: 70 },
        { coord: [120.163, 30.280], value: 44 },
        { coord: [120.164, 30.281], value: 60 },
        { coord: [120.165, 30.282], value: 50 },
        { coord: [120.166, 30.283], value: 46 },
        { coord: [120.167, 30.284], value: 75 },
        { coord: [120.168, 30.285], value: 62 },
        { coord: [120.169, 30.286], value: 69 },
        { coord: [120.170, 30.287], value: 61 },
        { coord: [120.171, 30.288], value: 71 },
        { coord: [120.172, 30.289], value: 63 },
        { coord: [120.173, 30.290], value: 68 },
        { coord: [120.174, 30.291], value: 79 },
        { coord: [120.175, 30.292], value: 47 },
      ],
      '华为': [
        { coord: [120.150, 30.250], value: 53 },
        { coord: [120.151, 30.251], value: 64 },
        { coord: [120.152, 30.252], value: 41 },
        { coord: [120.153, 30.253], value: 82 },
        { coord: [120.154, 30.254], value: 45 },
        { coord: [120.155, 30.255], value: 68 },
        { coord: [120.156, 30.256], value: 39 },
        { coord: [120.157, 30.257], value: 75 },
        { coord: [120.158, 30.258], value: 50 },
        { coord: [120.159, 30.259], value: 39 },
        { coord: [120.160, 30.260], value: 65 },
        { coord: [120.161, 30.261], value: 60 },
        { coord: [120.162, 30.262], value: 54 },
        { coord: [120.163, 30.263], value: 41 },
        { coord: [120.164, 30.264], value: 83 },
        { coord: [120.165, 30.265], value: 91 },
        { coord: [120.166, 30.266], value: 72 },
        { coord: [120.167, 30.267], value: 44 },
        { coord: [120.168, 30.268], value: 66 },
        { coord: [120.169, 30.269], value: 51 },
        { coord: [120.170, 30.270], value: 38 },
        { coord: [120.171, 30.271], value: 61 },
        { coord: [120.172, 30.272], value: 73 },
        { coord: [120.173, 30.273], value: 55 },
        { coord: [120.174, 30.274], value: 49 },
        { coord: [120.175, 30.275], value: 62 },
        { coord: [120.176, 30.276], value: 78 },
        { coord: [120.177, 30.277], value: 54 },
        { coord: [120.178, 30.278], value: 69 },
        { coord: [120.179, 30.279], value: 81 },
        { coord: [120.180, 30.280], value: 56 },
        { coord: [120.181, 30.281], value: 47 },
        { coord: [120.182, 30.282], value: 72 },
        { coord: [120.183, 30.283], value: 65 },
        { coord: [120.184, 30.284], value: 54 },
        { coord: [120.185, 30.285], value: 49 },
        { coord: [120.186, 30.286], value: 39 },
        { coord: [120.187, 30.287], value: 58 },
        { coord: [120.188, 30.288], value: 62 },
        { coord: [120.189, 30.289], value: 70 },
        { coord: [120.190, 30.290], value: 44 },
        { coord: [120.191, 30.291], value: 60 },
        { coord: [120.192, 30.292], value: 50 },
      ]
    }

    return {
      code: 200,
      message: 'success',
      data: data
    }
  })
}

// 手机流量排名数据
export const mockFlowRank = () => {
  Mock.mock(/\/api\/phone\/flow-rank/, 'get', () => {
    return {
      code: 200,
      message: 'success',
      data: {
        dimensions: ['手机型号', '总流量', 'Wi-Fi', '移动数据'],
        source: [
          ['product', 'ALL', 'Wi-Fi', 'mobile'],
          ['iPhone 14', 320, 302, 301],
          ['iPhone 13', 220, 182, 191],
          ['Huawei P60', 150, 232, 201],
          ['OPPO Find X6', 320, 332, 301],
          ['Xiaomi 13', 820, 932, 901]
        ]
      }
    }
  })
}

// 饼图数据模拟
export const mockFlowPie = () => {
  Mock.mock(/\/api\/phone\/flow-pie/, 'get', () => {
    return {
      code: 200,
      message: 'success',
      data: {
        pieData: [
          { value: 1048, name: 'iPhone 14' },
          { value: 735, name: 'iPhone 13' },
          { value: 580, name: 'Huawei P60' },
          { value: 484, name: 'OPPO Find X6' },
          { value: 300, name: 'Xiaomi 13' }
        ]
      }
    }
  })
}

// 调用 mock 函数以启用模拟数据
mockFlowRank()
mockFlowPie()

// 操作系统流量排名数据
export const mockOsRank = () => {
  Mock.mock(/\/api\/phone\/os-rank/, 'get', (options) => {
    // 解析 URL 参数
    const url = new URL(options.url, window.location.origin)
    const params = Object.fromEntries(url.searchParams)

    // 定义操作系统版本
    const osVersions = [
      'android4.1.2',
      'android4.3',
      'android2.3.7',
      'android4.1.1',
      'iOS 15.0',
      'iOS 14.8',
      'android5.0',
      'android6.0'
    ]

    // 生成不同类型的流量数据
    const generateData = (base) => {
      return osVersions.map(() => Mock.Random.float(base * 0.8, base * 1.2, 1, 1))
    }

    // 根据运营商和网络类型筛选数据
    const getSeriesData = () => {
      const allData = generateData(80)
      const wifiData = generateData(70)
      const mobileData = generateData(60)

      // 根据运营商筛选
      if (params.operator !== 'ALL') {
        const factor = params.operator === 'CMCC' ? 1.2 : (params.operator === 'CTCC' ? 0.9 : 1.0)
        return [
          allData.map(v => v * factor),
          wifiData.map(v => v * factor),
          mobileData.map(v => v * factor)
        ]
      }

      return [allData, wifiData, mobileData]
    }

    return {
      code: 200,
      message: 'success',
      data: {
        dimensions: ['操作系统', '总流量', 'Wi-Fi', '移动数据'],
        source: [
          ['product', 'ALL', 'Wi-Fi', 'mobile'],
          ...osVersions.map((os, index) => [
            os,
            ...getSeriesData().map(data => data[index])
          ])
        ]
      }
    }
  })
}

// 操作系统流量分布 - 饼图
export const mockOsPie = () => {
  Mock.mock(/\/api\/phone\/os-pie/, 'get', (options) => {
    // 解析 URL 参数
    const url = new URL(options.url, window.location.origin)
    const params = Object.fromEntries(url.searchParams)

    // 定义操作系统类型
    const osTypes = [
      'Android',
      'iOS',
      'HarmonyOS',
      'Windows Phone',
      'Others'
    ]

    // 生成流量数据
    const generatePieData = () => {
      const total = 100
      let remaining = total
      const result = []

      for (let i = 0; i < osTypes.length - 1; i++) {
        const value = i === 0 ? Mock.Random.float(30, 40, 1, 1) :
                     i === 1 ? Mock.Random.float(25, 35, 1, 1) :
                     Mock.Random.float(5, 15, 1, 1)
        result.push({
          value: value,
          name: osTypes[i]
        })
        remaining -= value
      }

      // 添加其他类型，确保总和为100
      result.push({
        value: parseFloat(remaining.toFixed(1)),
        name: osTypes[osTypes.length - 1]
      })

      return result
    }

    return {
      code: 200,
      message: 'success',
      data: {
        pieData: generatePieData()
      }
    }
  })
}

// 调用 mock 函数以启用模拟数据
mockOsRank()
mockOsPie()
